//
// SerialConnectPref.m
// SerialConnect
//
// Created by David Haselberger.
// Copyleft 2008 __SerialConnect__.
//
// SerialConnectPref.m is part of SerialConnect.
//
// SerialConnect is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// SerialConnect is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with SerialConnect.  If not, see <http://www.gnu.org/licenses/>.
//
//

#import "SerialConnectPref.h"


@implementation SerialConnectPref
- (id)initWithBundle:(NSBundle *)bundle
{
    if ( ( self = [super initWithBundle:bundle] ) != nil ) {
        appID = CFSTR("com.serialconnect.SerialConnect");
    }
	
    return self;
}

- (void) mainViewDidLoad
{
	CFPropertyListRef devString;
	CFPropertyListRef baudString;
	
    devString = CFPreferencesCopyAppValue( CFSTR("deviceNameString"),  appID);
    if ( devString && CFGetTypeID(devString) == CFStringGetTypeID()  ) {
        [deviceNameTextField setStringValue:(NSString*)devString];
    } else {
		[deviceNameTextField setStringValue:nil];   
	}
    if ( devString ) CFRelease(devString);
	
    baudString = CFPreferencesCopyAppValue( CFSTR("baudRateString"),  appID);
    if ( baudString && CFGetTypeID(baudString) == CFStringGetTypeID()  ) {
        [baudRateButton selectItemWithTitle:(NSString*)baudString];
    } else {
        [baudRateButton selectItemWithTitle:@"9600"];
	}
	if ( baudString ) CFRelease(baudString);
	
}

- (IBAction)setDeviceNameAndBaudRate:(id)sender
{
	if([deviceNameTextField stringValue])
	{
		CFPreferencesSetAppValue( CFSTR("deviceNameString"),[deviceNameTextField stringValue], appID );
	}
	
	CFPreferencesSetAppValue( CFSTR("baudRateString"),[baudRateButton titleOfSelectedItem], appID );
	
	CFNotificationCenterRef center;
	
    CFPreferencesAppSynchronize( appID );
	
    center = CFNotificationCenterGetDistributedCenter();
    CFNotificationCenterPostNotification(center, CFSTR("Preferences Changed"), appID, NULL, TRUE);
}

- (void)didUnselect
{
	
}



@end
